/*
 *  Copyright (C) 2010 Takaji
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * TransactionTypeDetailPanel.java
 *
 * Created on Jan 2, 2010, 1:30:15 PM
 */
package dakside.hacc.modules.transactions.trxtypeman;

import dakside.hacc.core.models.Account;
import dakside.hacc.core.models.TransactionAction;
import dakside.hacc.core.models.TransactionType;

/**
 *
 * @author Takaji
 */
public class TransactionTypeDetailPanel extends javax.swing.JPanel {

    /** Creates new form TransactionTypeDetailPanel */
    public TransactionTypeDetailPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtCode = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        cboAction = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cboAccount = new javax.swing.JComboBox();
        btnCreate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        transactionActionTable1 = new dakside.hacc.modules.transactions.trxtypeman.TransactionActionTable();

        jLabel1.setText("Code");
        jLabel1.setName("jLabel1"); // NOI18N

        txtCode.setName("txtCode"); // NOI18N

        jLabel2.setText("Name");
        jLabel2.setName("jLabel2"); // NOI18N

        txtName.setName("txtName"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel3.setText("Action");
        jLabel3.setName("jLabel3"); // NOI18N

        cboAction.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Debit", "Credit" }));
        cboAction.setName("cboAction"); // NOI18N

        jLabel4.setText("Account");
        jLabel4.setName("jLabel4"); // NOI18N

        cboAccount.setName("cboAccount"); // NOI18N

        btnCreate.setText("Create");
        btnCreate.setName("btnCreate"); // NOI18N
        btnCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.setName("btnDelete"); // NOI18N
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        transactionActionTable1.setModel(new TransactionActionTableModel());
        transactionActionTable1.setName("transactionActionTable1"); // NOI18N
        jScrollPane1.setViewportView(transactionActionTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCode, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cboAction, 0, 301, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cboAccount, 0, 292, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnCreate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDelete)
                .addContainerGap(211, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cboAction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cboAccount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCreate)
                    .addComponent(btnDelete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateActionPerformed
        TransactionAction action = new TransactionAction();
        if (Account.TYPE_DEBIT_STRING.equals(cboAction.getSelectedItem())) {
            action.setAction(Account.TYPE_DEBIT);
        } else if (Account.TYPE_CREDIT_STRING.equals(cboAction.getSelectedItem())) {
            action.setAction(Account.TYPE_CREDIT);
        }
        action.setToAccount((Account) cboAccount.getSelectedItem());

        //add to model
        TransactionActionTableModel model = (TransactionActionTableModel) transactionActionTable1.getModel();
        model.add(action);
        model.fireTableDataChanged();
    }//GEN-LAST:event_btnCreateActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        transactionActionTable1.removeSelected();
    }//GEN-LAST:event_btnDeleteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCreate;
    private javax.swing.JButton btnDelete;
    private javax.swing.JComboBox cboAccount;
    private javax.swing.JComboBox cboAction;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private dakside.hacc.modules.transactions.trxtypeman.TransactionActionTable transactionActionTable1;
    private javax.swing.JTextField txtCode;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
    /**
     * Inner transaction type object
     */
    private TransactionType trxType = null;

    public TransactionType getTransactionType() {
        if (trxType == null) {
            trxType = new TransactionType();
        }

        if(txtCode.getText() == null || txtCode.getText().trim().length() == 0) return null;
        if(txtName.getText() == null || txtName.getText().trim().length() == 0) return null;

        trxType.setCode(txtCode.getText());
        trxType.setName(txtName.getText());

        trxType.getActions().clear();
        TransactionAction[] actions = transactionActionTable1.getAllActions();

        //TO DO throw exceptions
        if (actions.length < 2) return null;
        for (TransactionAction action : actions) {
            trxType.add(action);
        }
        return trxType;
    }

    public void setTransactionType(TransactionType trxType) {
        this.trxType = trxType;
        //reset UI
        txtCode.setText("");
        txtName.setText("");
        TransactionActionTableModel model = (TransactionActionTableModel) transactionActionTable1.getModel();
        model.clear();
        model.fireTableDataChanged();

        //XXX review this code later
        if (trxType != null) {
            txtCode.setText(trxType.getCode());
            txtName.setText(trxType.getName());
            transactionActionTable1.load(trxType.getActionArray());
        }
    }

    public void refresh() {
        Account[] accounts = TransactionTypeController.getInstance().getAllAccounts();
        cboAccount.removeAllItems();
        for (Account account : accounts) {
            if (account.getType() != Account.TYPE_FOLDER) {
                cboAccount.addItem(account);
            }
        }
    }
}
